/*
 * @Title: 拼团首页
 * @Author: zhj
 * @Date:   2018-10-24
 * @Last Modified by:   zhj
 * @Last Modified time: 2018-10-24
 */
var dataInfo = {},
  template = {
    init: function() {
      Vue.use(VueLazyload, {
        preLoad: 1.3,
        error: 'https://h5.meetao.com/img/product/init.png',
        loading: 'https://h5.meetao.com/img/product/init.png',
        attempt: 1
      });
      dataInfo.init = new Vue({
        el: '#app',
        data: {
          all: false, //loadOver - show
          loading: false,
          isLoading: false,
          finished: false,
          pullShow: true,
          GroupType: "", //主商品ID类型,需要从首页链接中截取
          pageNo: 1, //页数
          pageSize: 10,
          PtListData: [], //拼团列表
          noData: false, //无数据显示
          type: "", //拼团规则  /*1:新人，2：普通，3：团长*/
          icon: {
            normal: '../img/index/shouhui.png',
            active: '../img/index/shouhong.png',
            normal1: '../img/index/gouhui.png',
            active1: '../img/index/gouhong.png',
            active2: '../img/index/myhong.png',
            normal2: '../img/index/myhui.png',
            active3: '../img/index/pthong.png',
            normal3: '../img/index/pthui.png',
          },
          active: 1, //taber开关
          tabbar_show: "" //判断是否显示tabber
        },
        methods: {
          init: function() {
            if (getQueryString("GroupType") == "common_pt_groupon") {
              dataInfo.init.tabbar_show = true;
            } else {
              dataInfo.init.tabbar_show = false;
            }
          },
          onLoad: function() {
            this.getgrouplist();
          },
          onRefresh: function() { //下拉刷新
            this.PtListData = [];
            this.pageNo = 1;
            dataInfo.init.all = false;
            setTimeout(function() {
              if (dataInfo.init.PtListData.length == 0) {
                dataInfo.init.getgrouplist();
              }
            }, 500);
          },
          getgrouplist: function() { //获取拼团商品信息
            if (getQueryString("GroupType") == null) {
              dataInfo.init.GroupType = "common_pt_groupon";
            } else {
              dataInfo.init.GroupType = getQueryString("GroupType");
            };
            ajaxTemplate(addressTest + SERVICE.getgrouplist, function(data) {
              var daTa = data.data.result;
              if (data.tag == 0) {
                dataInfo.init.PtListData = JSON.parse(JSON.stringify(dataInfo.init.PtListData.concat(daTa)));
                //								if(daTa.length == 0 && dataInfo.init.PtListData.length == 0) {
                //									dataInfo.init.noData = true;
                //									dataInfo.init.all = false;
                //									dataInfo.init.pullShow = false;
                //								} else {
                //									if(daTa.length < dataInfo.init.pageSize) {
                //										if(daTa.length == 0) {
                //											dataInfo.init.noData = true;
                //											dataInfo.init.finished = false;
                //											dataInfo.init.pullShow = false;
                //										} else {
                //											dataInfo.init.finished = true;
                //											dataInfo.init.all = true;
                //											dataInfo.init.loading = false;
                //										};
                //									} else {
                //										dataInfo.init.finished = false;
                //									};
                //									dataInfo.init.join_people();
                //								}
                if (daTa.length < dataInfo.init.pageSize) {
                  dataInfo.init.finished = true;
                  dataInfo.init.PtListData.length == 0 ? dataInfo.init.noData = true : dataInfo.init.all = true;
                } else {
                  dataInfo.init.finished = false;
                };
                dataInfo.init.loading = false;
                dataInfo.init.isLoading = false;

              } else {
                dataInfo.init.noData = true;
                dataInfo.init.pullShow = false;
                dataInfo.init.$toast(data.errMsg);
              }
            }, {
              GroupType: dataInfo.init.GroupType,
              pageNo: dataInfo.init.pageNo++,
              pageSize: dataInfo.init.pageSize
            });
          },
          onPtProductDetails: function(productId, activityId, Stock) {
            if (Stock != 0) {
              location.href = "../html/PtProductDetails.html?productId=" + productId + "&activityId=" + activityId;
            } else {
              dataInfo.init.$toast("抢光啦");
            }
          },
          Onrule: function() { //跳转规则
            if (dataInfo.init.GroupType == "common_pt_groupon") {
              dataInfo.init.type = "2";
            } else if (dataInfo.init.GroupType == "new_guest_pt_groupon") {
              dataInfo.init.type = "1";
            } else if (dataInfo.init.GroupType == "colonel_free_pt_groupon") {
              dataInfo.init.type = "3";
            }
            location.href = "../html/PtRule.html?type=" + dataInfo.init.type;
          },
          join_people: function() { //参与人数截取位数
            for (var i = 0; i < dataInfo.init.PtListData.length; i++) {
              if (dataInfo.init.PtListData[i].showActorNum >= 10000) {
                dataInfo.init.PtListData[i].showActorNum = (dataInfo.init.PtListData[i].showActorNum / 10000).toFixed(1) + "万";
              }
            }
          }
        }
      });
      dataInfo.init.init();
    }
  };
template.init();
